package com.one.common.utils;

import android.content.Context;
import android.util.Log;

import com.one.common.config.MyContact;
import com.one.common.model.bean.PayWXBean;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.modelpay.PayReq;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

/**
 * author ：zhulunjun
 * email  ：zhulunjun@qq.com
 * date   ：2018/10/27 11:33 PM
 * description ：
 **/
public class PayUtils {


    private static PayUtils mPayHelper = null;
    private Context mContext;

    private IPayListener mIPayListener;

    public PayUtils setIPayListener(IPayListener mIPayListener) {
        this.mIPayListener = mIPayListener;
        return this;
    }

    public interface IPayListener {
        void onSuccess();

        void onFail();

        void onCancel();

    }

    public static PayUtils getInstance(Context context) {
        if (mPayHelper == null) {
            mPayHelper = new PayUtils(context);
        }
        return mPayHelper;
    }

    public PayUtils(Context mContext) {
        this.mContext = mContext;
    }


    /**
     * 设置微信回调
     */
    public void setWxPayResp(BaseResp resp) {
        int code = resp.errCode;
        String transaction = resp.transaction;
        String errStr = resp.errStr;

        Log.i("dfc", "code: " + code + "");
        Log.i("dfc", "transaction: " + transaction);
        Log.i("dfc", "errStr: " + errStr);
        if (code == 0) {
            if (mIPayListener != null) {
                mIPayListener.onSuccess();
            }

        } else if (code == -1) {
            if (mIPayListener != null) {
                mIPayListener.onFail();
            }
        } else if (code == -2) {
            if (mIPayListener != null) {
                mIPayListener.onCancel();
            }

        } else {
            if (mIPayListener != null) {
                mIPayListener.onFail();
            }
        }
    }


    /**
     * 调起微信支付
     */
    private IWXAPI msgApi = null;

    public PayUtils wxPay(PayWXBean wxpayMode) {

        if (msgApi == null) {
            Logger.d("注册到微信 ");
            msgApi = WXAPIFactory.createWXAPI(mContext, null);
            msgApi.registerApp(MyContact.WX_KEY);// 将该app注册到微信
        }
        PayReq req = new PayReq();
        if (!msgApi.isWXAppInstalled()) {
            Toaster.showLongToast("手机中没有安装微信客户端", MyToast.FAIL);

            return this;
        }
        if (wxpayMode != null) {

            req.appId = wxpayMode.getAppid();
            req.partnerId = wxpayMode.getPartnerid();
            req.prepayId = wxpayMode.getPrepayid();
            req.nonceStr = wxpayMode.getNoncestr();
            req.timeStamp = wxpayMode.getTimestamp() + "";
            req.packageValue = wxpayMode.getPackageX();
            req.sign = wxpayMode.getSign();
            // req.extData = MaiLiApplication.getInstance().getUserInfo().getPhone();  微信拓展参数
            msgApi.sendReq(req);
            Logger.d("发送支付请求 ");
        }
        return this;
    }

}
